// const path = require('path');
// const fs = require("fs");
import fs from 'fs'
import path from 'path'

const getDirectory = (pre, baseUrl) => {
    let fileOrDir = {
        dir: [],
        file: [],
    }
    const dirName = path.resolve(process.cwd(), baseUrl);
    fs.readdirSync(dirName).forEach(dirItem => {
        //读取文件
        let joinPath = path.join(dirName, dirItem)

        let stat = fs.statSync(joinPath);
        if (stat.isDirectory()) {
            fileOrDir.dir.push(dirItem)
        } else {
            fileOrDir.file.push(dirItem);
        }
    })

    let obj = {};
    fileOrDir.dir.forEach(dir => {
        obj[`${pre}${dir}`] = path.join(process.cwd(), baseUrl, dir)
    })
    return obj
}
/**
 *
 * @param prf 前缀  默认@
 * @returns {{config(*, {mode: *, command: *}): void}}
 */
export default  (prf = "@", baseUrl = "./src") => {
    return {
        name: 'lxl-vite-aliases',
        config(config, {mode, command}) {
            // console.log('mode', mode);
            // console.log('command', command);
            // console.log('configconfigconfig', config);
            // console.log(fs);
            return {
                resolve: {
                    alias: getDirectory(prf, baseUrl)
                }
            }
        }
    }
}
